package model.menupages;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import view.Master;
import view.menuelements.MenuButton;
import view.menuelements.MenuLabel;
import view.menuelements.Menupanel;
import controller.Heytetrisapplication;
import controller.Highscorecontroller;

public class Highscorepage extends JFrame {
	URL ressource1 = ClassLoader.getSystemResource("Highscore_Transparent.png");
	Icon icon1 = new ImageIcon(ressource1);
	JLabel centerpic = new JLabel(icon1);
	private Menupanel Scorepanel = new Menupanel();
	private Menupanel Scorepanel1 = new Menupanel();
	private Menupanel Scorepanel2 = new Menupanel();
	// private MenuLabel Scorefill = new MenuLabel("");
	// private MenuLabel Scorefill1 = new MenuLabel("");
	private MenuLabel ueberschrift1 = new MenuLabel("Platz");
	private MenuLabel ueberschrift2 = new MenuLabel("Name");
	private MenuLabel ueberschrift3 = new MenuLabel("Punktzahl");
	MenuButton resetbutton = new MenuButton("Reset", 100, 50);
	MenuButton backbutton = new MenuButton("", 100, 50);
	Menupanel buttons = new Menupanel();
	Highscorepage hs = this;

	public Highscorepage() throws HeadlessException {
		super();
		Master.setStandardpageOptions(this);
		Master.addCloseButton(this);
		add(Scorepanel, BorderLayout.CENTER);
		Scorepanel1.setLayout(new FlowLayout(FlowLayout.CENTER));
		Scorepanel2.setLayout(new GridLayout(15, 3));

		// Scorepanel.add(Scorefill);
		Scorepanel1.add(centerpic);
		// Scorepanel.add(Scorefill1);
		Scorepanel2.add(ueberschrift1);
		Scorepanel2.add(ueberschrift2);
		Scorepanel2.add(ueberschrift3);
		Scorepanel.add(Scorepanel1);
		Scorepanel.add(Scorepanel2);
		final Highscorecontroller hscontroller = new Highscorecontroller();
		Dimension dimension = new Dimension(500, 400);
		Scorepanel2.setPreferredSize(dimension);
		Scorepanel2.setMinimumSize(dimension);
		Scorepanel2.setMaximumSize(dimension);

		for (int i = 0; i < hscontroller.getLaenge(); i++) {
			Scorepanel2.add(new MenuLabel(i + 1 + ""));
			Scorepanel2.add(new MenuLabel(hscontroller.getName(i)));
			Scorepanel2.add(new MenuLabel(hscontroller.getScore(i) + ""));
		}

		// legt die Liste mit den Highscores an

		// Fuegt einen Highscore als Test hinzu
		// hscontroller.addHighscore("test",100);

		// String[] scorelist = new String[hscontroller.getLaenge()];
		// for (int i = 0; i < hscontroller.getLaenge(); i++) {
		// scorelist[i] = "<html><pre>" + (i + 1) + ":\t";
		// scorelist[i] += hscontroller.getName(i) + "\t\t\t\t";
		// scorelist[i] += hscontroller.getScore(i) + "";
		// scorelist[i] += "</pre></html>";
		// MenuLabel scorelabel = new MenuLabel(scorelist[i]);
		//
		// Scorepanel.add(scorelabel);
		//
		// }

		URL ressource = ClassLoader.getSystemResource("BACK.png");
		backbutton.setIcon(new ImageIcon(ressource));
		buttons.add(resetbutton);
		buttons.add(backbutton);

		resetbutton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				hscontroller.highscoresloeschen();
				Heytetrisapplication.highscorepushed();
				Master.setPageInvisible(hs);
			}
		});

		backbutton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Heytetrisapplication.hauptmenupushed();
				Master.setPageInvisible(hs);
			}
		});

		add(buttons, BorderLayout.SOUTH);
		// Master.addBackbutton(this);
		Master.setPageVisible(this);
	}

}
